<?php
//商品类型控制器
class TypeController extends BaseController {
	//显示商品类型
	public function indexAction(){
		//获取所有的商品类型
		$typeModel = new TypeModel('goods_type');
		//$types = $typeModel->getTypes();
		$pagesize = 3; //每页显示的条数，可以在配置文件中配置
	 	$current = isset($_GET['page']) ? $_GET['page'] : 1 ;//获取当前页
	 	$offset = ($current - 1) * $pagesize; //计算偏移量
	 	//调用模型取数据
	 	$types = $typeModel->getPageTypes($offset,$pagesize);
	 	//载入分页类
	 	$this->library('Page');
	 	//获取总的记录数
	 	$where = "";
	 	$total = $typeModel->total($where);
	 	//实例化
	 	$page = new Page($total,$pagesize,$current,'index.php',
	 		array('p'=>'admin','c'=>'type','a'=>'index'));
	 	//调用方法获取分页信息
	 	$pageinfo = $page->showPage();
		//载入视图
		include CUR_VIEW_PATH . "goods_type_list.html";
	}
	//显示添加表单页面
	public function addAction(){
		include CUR_VIEW_PATH . "goods_type_add.html";
	}
	//完成添加动作
	public function insertAction(){
		//1.收集表单数据
		$data['type_name'] = trim($_POST['type_name']);
		//2.验证和处理
		if ($data['type_name'] == '') {
			$this->jump('index.php?p=admin&c=type&a=add','商品类型名称不能为空');
		}
		//转义处理
		$this->helper('input');
		$data = deepslashes($data);
		$data = deepspecialchars($data);
		//3.调用模型完成入库操作，提示并跳转
		$typeModel = new TypeModel('goods_type');
		if ($typeModel->insert($data)) {
			$this->jump('index.php?p=admin&c=type&a=index','添加类型成功',2);
		} else {
			$this->jump('index.php?p=admin&c=type&a=add','添加类型失败');
		}
	}
	//显示修改表单页面
	public function editAction(){
		include CUR_VIEW_PATH . "goods_type_edit.html";
	}
	//完成更新操作
	public function updateAction(){}
	//删除操作
	public function deleteAction(){}
}